package com.yoomiito.app.widget.songduoduo

import android.app.Activity
import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import cn.droidlover.xdroidmvp.router.Router
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.WebViewActivity
import com.yoomiito.app.model.sdd.SddUserData
import com.yoomiito.app.ui.my.songdudouo.GiveMoreIncomeActivity
import com.yoomiito.app.utils.ColorUtils
import com.yoomiito.app.utils.Const
import com.yoomiito.app.utils.DensityUtil
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.view_giver_data.view.*
import java.util.concurrent.TimeUnit

class GiverDataView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    :LinearLayout(context, attrs, defStyleAttr, defStyleRes){
    private val view = LayoutInflater.from(context).inflate(R.layout.view_giver_data,null,false)
    init {

        val lp = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
        addView(view,lp)
        tipTv.background = ColorUtils.gradientAndRadius("#FFFF246C", "#FFFF035B", DensityUtil.dp2px(6f).toFloat())
        cumulativeIncomeLl.setOnClickListener { GiveMoreIncomeActivity.start(context) }
        detailDes.setOnClickListener {   Router.newIntent(context as Activity)
                .to(WebViewActivity::class.java)
                .putInt(Constant.TYPE, Const.Url.SddVipRights)
                .launch() }

        initTip()
    }

    private fun initTip() {
        Observable.timer(5000,TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    tip.visibility = View.GONE
                }

    }

    fun updateData(data: SddUserData){
        tv1.text = data.leftNum.toString()
        tv2.text = data.sendNum.toString()
        tv3.text = data.pendingConfirmNum.toString()
        val money = when {
            data.snedMoreIncome.indexOf(".")==-1 -> {
                data.snedMoreIncome
            }
            data.snedMoreIncome.indexOf(".")>5 -> {
                data.snedMoreIncome.substring(0,data.snedMoreIncome.indexOf("."))
            }
            else -> {
                data.snedMoreIncome
            }
        }
        val sp = SpannableString("¥$money")
        sp.setSpan(RelativeSizeSpan(0.65f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        tv4.text = sp
    }

}